package cn.listener;

import cn.hutool.core.util.RandomUtil;
import cn.wetcland.item.RandomDropItem;
import cn.wetcland.mob.LevelMonster;
import org.bukkit.World;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.inventory.ItemStack;

import java.util.List;

public class EntityEventListener implements Listener {

    LevelMonster levelMonster = new LevelMonster();

    @EventHandler
    public void EntitySpawnEvent(EntitySpawnEvent event) {
        Entity entity = event.getEntity();
        // 主城不生成怪物
        if (entity.getWorld().getName().equals("world")) {
            if (entity.getType() == EntityType.ARMOR_STAND) {
                return;
            } else {
                event.setCancelled(true);
            }
        }
        // 如果该实体是怪物
        if (entity instanceof PigZombie || entity instanceof Drowned || entity instanceof Zoglin ||
                entity instanceof ZombieVillager || entity instanceof Husk || entity instanceof Stray ||
                entity instanceof Zombie || entity instanceof WitherSkeleton || entity instanceof Skeleton ||
                entity instanceof Spider || entity instanceof Creeper || entity instanceof Enderman ||
                entity instanceof Piglin || entity instanceof Blaze || entity instanceof ElderGuardian ||
                entity instanceof Guardian || entity instanceof Endermite || entity instanceof Evoker ||
                entity instanceof Ghast || entity instanceof MagmaCube || entity instanceof Slime ||
                entity instanceof Vex || entity instanceof Vindicator || entity instanceof Witch) {
            levelMonster.setMonster((Mob) entity);
            int randomInt = RandomUtil.randomInt(1, 101);
            if (randomInt >= 99) {
                // 1% 几率 十阶怪物
                levelMonster.modifyOf10();
            } else if (randomInt >= 98) {
                // 2% 几率 九阶怪物
                levelMonster.modifyOf9();
            } else if (randomInt >= 97) {
                // 3% 几率 八阶怪物
                levelMonster.modifyOf8();
            } else if (randomInt >= 96) {
                // 4% 几率 七阶怪物
                levelMonster.modifyOf7();
            } else if (randomInt >= 95) {
                // 5% 几率 六阶怪物
                levelMonster.modifyOf6();
            } else if (randomInt >= 94) {
                // 6% 几率 五阶怪物
                levelMonster.modifyOf5();
            } else if (randomInt >= 93) {
                // 7% 几率 四阶怪物
                levelMonster.modifyOf4();
            } else if (randomInt >= 92) {
                // 8% 几率 三阶怪物
                levelMonster.modifyOf3();
            } else if (randomInt >= 91) {
                // 9% 几率 二阶怪物
                levelMonster.modifyOf2();
            } else if (randomInt >= 90) {
                // 10% 几率 一阶怪物
                levelMonster.modifyOf1();
            } else {
                // 90% 普通怪物
            }
        }
    }

    @EventHandler
    public void EntityDeathEvent(EntityDeathEvent event) {
        LivingEntity entity = event.getEntity();
        World world = entity.getWorld();
        // 处理等级怪物 物品掉落
        if (entity instanceof Zoglin ||
                entity instanceof Zombie || entity instanceof Skeleton ||
                entity instanceof Spider || entity instanceof Creeper || entity instanceof Enderman ||
                entity instanceof Piglin || entity instanceof Blaze || entity instanceof Guardian || entity instanceof Endermite || entity instanceof Evoker ||
                entity instanceof Ghast || entity instanceof Slime ||
                entity instanceof Vex || entity instanceof Vindicator || entity instanceof Witch) {
            String customName = entity.getCustomName();
            if (customName == null || !customName.startsWith("§c")) return;
            char c = customName.charAt(3);
            List<ItemStack> levelMonsterDropItem;
            switch (c) {
                case '一':
                    levelMonsterDropItem = RandomDropItem.getLevelMonsterDropItem(null, 1);
                    break;
                case '二':
                    levelMonsterDropItem = RandomDropItem.getLevelMonsterDropItem(null, 2);
                    break;
                case '三':
                    levelMonsterDropItem = RandomDropItem.getLevelMonsterDropItem(null, 3);
                    break;
                case '四':
                    levelMonsterDropItem = RandomDropItem.getLevelMonsterDropItem(null, 4);
                    break;
                case '五':
                    levelMonsterDropItem = RandomDropItem.getLevelMonsterDropItem(null, 5);
                    break;
                case '六':
                    levelMonsterDropItem = RandomDropItem.getLevelMonsterDropItem(null, 6);
                    break;
                case '七':
                    levelMonsterDropItem = RandomDropItem.getLevelMonsterDropItem(null, 7);
                    break;
                case '八':
                    levelMonsterDropItem = RandomDropItem.getLevelMonsterDropItem(null, 8);
                    break;
                case '九':
                    levelMonsterDropItem = RandomDropItem.getLevelMonsterDropItem(null, 9);
                    break;
                case '十':
                    levelMonsterDropItem = RandomDropItem.getLevelMonsterDropItem(null, 10);
                    break;
                default:
                    throw new IllegalStateException("Unexpected value: " + c);
            }
            for (ItemStack itemstack : levelMonsterDropItem) {
                world.dropItem(entity.getLocation(), itemstack);
            }
        }
    }

    @EventHandler
    public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
        Entity entity = event.getEntity();
        Entity damager = event.getDamager();
        String customName = entity.getCustomName();
        if (customName != null) {
            if (damager.isOp()) return;
            switch (customName) {
                case "§d[至尊]§b强化大师":
                    damager.sendMessage("§d[至尊]§b强化大师: §c竟敢对我无礼？");
                    event.setCancelled(true);
                    break;
                case "§d[至尊]§b淬炼大师":
                    damager.sendMessage("§d[至尊]§b淬炼大师: §c竟敢对我无礼？");
                    event.setCancelled(true);
                    break;
                case "§d[至尊]§b等级大师":
                    damager.sendMessage("§d[至尊]§b等级大师: §c竟敢对我无礼？");
                    event.setCancelled(true);
                    break;
            }
        }
    }

}
